import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Iterator;

public class Game
{
    private Player _player1;
    private Player _player2;
    private Player _currentPlayer;
    private Board _theBoard;
    private Dictionary _dictionary;
    private long _startTime;
    private char[] _alphabet;
    private int _numTiles;
    int _currentRound;
 
    
    public Game(String fileName, int numTiles, char[] alphabet) throws FileNotFoundException 
    {     
    
        _numTiles = numTiles;
        _alphabet = alphabet;
        
        _player1 = new Player(1);
        _player2 = new Player(2);
        
        _currentPlayer = _player1;
        
        try
        {
            _theBoard = new Board(numTiles, alphabet);
        } 
        catch (Exception e)
        {
            throw new IllegalArgumentException ("Board failed to initialize");
        }
        
        _dictionary = new Dictionary(fileName);

        _currentRound = 1;
    }
  
    
    /**
     * 
     * @return the currentPlayer
     */
    public Player getCurrentPlayer()
    {   
        return _currentPlayer;
    }
    
    
    /**
     * 
     */
    public void switchPlayer()
    {
        if(_currentPlayer == _player1)
        {
            _currentPlayer = _player2;
        }
        else
        {
            _currentPlayer = _player1;
        }
    }
    
    
    /**
     * 
     * @return player1
     */
    public Player getPlayer1()
    {   
        return _player1;
    }
    
    
    /**
     * 
     * @return player2
     */
    public Player getPlayer2()
    {   
        return _player2;
    }
    
    
    /**
     * 
     * @return currentRound
     */
    public int getCurrentRound()
    {      
        return _currentRound;
    }
    
    
    /**
     * 
     * @return theBoard
     */
    public Board getBoard()
    {
        return _theBoard;
    }
    
    
    /**
     * Creates a new board for next round
     * @throws Exception 
     */
    public void startNextRound() throws Exception
    {
        _theBoard = new Board(_numTiles, _alphabet);
        score();
        _currentRound ++;
    }
    
    public void startNewGame()
    {
    	_player1 = new Player(1);
        _player2 = new Player(2);
        
        _currentPlayer = _player1;
        
        try
        {
            _theBoard = new Board(_numTiles, _alphabet);
        } 
        catch (Exception e)
        {
            throw new IllegalArgumentException ("Board failed to initialize");
        }
        
        _currentRound = 1;
    }
    
    /**
     * 
     * @param word
     * @return true if word exists
     */
    public boolean submitWord(String word)
    {
       boolean wordExists = false;

       if(_theBoard.doesExist(word))
       {
           return _currentPlayer.makeMove(word);
         
       }
       
       return wordExists;
      }
    
    
    /**
     * calculate score
     */
    public void score()
    {  
        scorePlayer(_player1 , _player2);
        scorePlayer(_player2 , _player1);
    }
    
    
    /**
     * @return 
    * 
    */
    private void scorePlayer(Player scorePlayer, Player comparePlayer)
   {
       HashSet<String> words = new HashSet<String>();
        int wordlength = 0;
        
        words = scorePlayer.getWords();
        
        // create an iterator
        Iterator<String> iterator = words.iterator(); 

        // check values and calculate school
        while (iterator.hasNext())
        {
            String word = iterator.next();
                      
            if(_dictionary.validate(word) && !comparePlayer.getWords().contains(word))
            {
              
              if (word.length() > 2 || word.length() < 5)
              {
                  scorePlayer.addToScore(1);
              }
              else if (word.length() == 5)
              {
                  scorePlayer.addToScore(2);
              }
              else if (word.length() == 6)
              {
                  scorePlayer.addToScore(3); 
              }
              else if (word.length() == 7)
              {
                  scorePlayer.addToScore(4); 
              }
              else if (word.length() > 8)
              {
                  scorePlayer.addToScore(11); 
              }
            }
        }
    }       
}